package com.agua.enturno.vista.componentes;

import com.agua.enturno.modelo.turno.EstadoDeTurno;
import com.agua.enturno.vista.dto.HorarioDTO;
import com.agua.enturno.vista.eventos.HorarioSeleccionadoEvent;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;

public class HorarioGUI extends AjaxFallbackLink<HorarioDTO> {

	private static final long serialVersionUID = 1L;
	
	// Altura de cada minuto de duracion en pixeles
	public static final float MINUTE_HEIGHT = 0.6f; 

	
	/*------------------------------------ Constructors ------------------------------------*/

	public HorarioGUI(String id, IModel<HorarioDTO> model) {
		super(id, model);
		this.setOutputMarkupId(true);
	}

	/*--------------------------------------------------------------------------------------*/
	/*------------------------------------ Public methods ----------------------------------*/

	@Override
	public void onClick(AjaxRequestTarget target) {
		
		if ( elHorarioEsSeleccionable() )
			send(getPage(), Broadcast.BREADTH, new HorarioSeleccionadoEvent(getHorario(), target));
	}

	/*--------------------------------------------------------------------------------------*/
	/*---------------------------------- Protected methods ---------------------------------*/

	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		
		tag.put("style", 
			"background-color:" + getColor() + "; " +
			"height: " + getHeight() + "px; " + 
			"cursor: " + getCursor() + ";"
		);
		
		if (getHorario().getEstado().equals(EstadoDeTurno.DESHABILITADO)) {
			
			tag.put("rel", "popover");
			tag.put("title", "Turno deshabilitado");
			tag.put("data-content", getHorario().getComentarioDelEstado());
		}
		else {
			tag.put("rel", "tooltip");
			tag.put("title", getHorario().getComentarioDelEstado());
		}
	}

	/*--------------------------------------------------------------------------------------*/
	/*----------------------------------- Private methods ----------------------------------*/

	private String getColor() {

		String color = null;

		switch (getHorario().getEstado()) {
		case LIBRE:
			color = "#B6D7A8";
			break;
		case OCUPADO:
			color = "#EA9999";
			break;
		case DESHABILITADO:
			color = "#FFE599";
			break;
		case INEXISTENTE:
			color = "#EEF3F7";
			break;
		}
		
		return color;
	}
	
	private String getHeight() {
		
		float height = getHorario().getIntervalo().toDuration().getStandardMinutes() * MINUTE_HEIGHT;
		return String.valueOf(height);
	}
	
	private String getCursor() {
		
		if ( elHorarioEsSeleccionable() )
			return "pointer";
		else
			return "default";
	}

	private HorarioDTO getHorario() {
		return (HorarioDTO) getDefaultModel().getObject();
	}
	
	private boolean elHorarioEsSeleccionable() {
		return getHorario().getEstado().equals(EstadoDeTurno.LIBRE);
	}

	/*--------------------------------------------------------------------------------------*/
	
}
